package edu.hgnc.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;


// redirect 和forward 请求重定向和和请求转发的区别，post请求转发的request不能传递form-data表单数据，(可以用url传参，但不推荐)，此时可以用flashMap传参
@Controller
public class FlashMapController {

	private static final Log logger = LogFactory.getLog(FlashMapController.class);

	@RequestMapping(value = "/submit",method = RequestMethod.POST)
	public String submit(RedirectAttributes attributes){
		logger.info("======================================================");
		logger.info("hello,when you see me,that's mean you are test succeed~");
		logger.info("======================================================");

		//使用RequestContextHolder获得FlashMap
		// FlashMap flashMap = (FlashMap) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
		// flashMap.put("name","杨戬-yangjian");
		((FlashMap) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE))
				.put("name","杨戬-yangjian");
		// 放到flashMap中，同时也设置到model中
		attributes.addFlashAttribute("orderID","2017310240109");
		// 将参数拼接到url中
		attributes.addAttribute("local","china");

		// 获取flashMap中的值
		HttpServletRequest request = ((ServletRequestAttributes)(RequestContextHolder.getRequestAttributes())).getRequest();
		FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
		logger.warn("name-->"+outputFlashMap.get("name"));
		logger.warn("orderID-->"+outputFlashMap.get("orderID"));
		logger.warn("local-->"+outputFlashMap.get("local"));

		Object attribute = request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
		logger.warn("attribute==>"+attribute);

		return "redirect:showOrders";
	}

	@RequestMapping(value = "/showOrders",method = RequestMethod.GET)
	public String showoOrders(Model model){
		logger.info("======================================================");
		logger.info("hello,when you see me,that's mean you are test succeed~");
		logger.info("======================================================");
		logger.warn("name==>"+model.getAttribute("name"));
		logger.warn("orderID==>"+model.getAttribute("orderID"));
		logger.warn("local==>"+model.getAttribute("local"));

		return "order";
	}
}
